package cn.edu.tju.elm.controller;

import cn.edu.tju.core.model.HttpResult;
import cn.edu.tju.core.model.User;
import cn.edu.tju.core.security.service.UserService;
import cn.edu.tju.elm.dto.ReviewDto;
import cn.edu.tju.elm.model.Review;
import cn.edu.tju.elm.service.ReviewService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/reviews")
@Tag(name = "评价管理", description = "提供评价的增删改查接口")
public class ReviewController {

    @Autowired
    private UserService userService;

    @Autowired
    private ReviewService reviewService;

    @PostMapping("")
    @Operation(summary = "创建评价", description = "用户对订单或商品进行评价")
    public HttpResult<Review> createReview(@RequestBody ReviewDto reviewDto) {
        // 获取当前登录用户
        User currentUser = userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));

        // 调用服务层创建评价
        Review newReview = reviewService.createReview(reviewDto, currentUser);
        
        return HttpResult.success(newReview);
    }

    @GetMapping("/order/{orderId}")
    @Operation(summary = "获取订单评价", description = "根据订单ID获取评价")
    public HttpResult<ReviewDto> getReviewByOrderId(@PathVariable Long orderId) {
        // 获取当前登录用户
        userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));

        // 调用服务层获取评价
        ReviewDto reviewDto = reviewService.getReviewByOrderId(orderId);
        
        return HttpResult.success(reviewDto);
    }

    @GetMapping("/user")
    @Operation(summary = "获取用户的评价", description = "获取当前用户的所有评价")
    public HttpResult<List<ReviewDto>> getMyReviews() {
        // 获取当前登录用户
        User currentUser = userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));

        // 调用服务层获取用户的评价
        List<ReviewDto> reviews = reviewService.getReviewsByUserId(currentUser.getId());
        
        return HttpResult.success(reviews);
    }

    @GetMapping("/business/{businessId}")
    @Operation(summary = "获取商家评价", description = "根据商家ID获取评价列表")
    public HttpResult<List<ReviewDto>> getBusinessReviews(@PathVariable Long businessId) {
        // 获取当前登录用户
        userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));

        // 调用服务层获取商家的评价
        List<ReviewDto> reviews = reviewService.getReviewsByBusinessId(businessId);
        
        return HttpResult.success(reviews);
    }

    @GetMapping("/food/{foodId}")
    @Operation(summary = "获取商品评价", description = "根据商品ID获取评价列表")
    public HttpResult<List<ReviewDto>> getFoodReviews(@PathVariable Long foodId) {
        // 获取当前登录用户
        userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));

        // 调用服务层获取商品的评价
        List<ReviewDto> reviews = reviewService.getReviewsByFoodId(foodId);
        
        return HttpResult.success(reviews);
    }

    @DeleteMapping("/{reviewId}")
    @Operation(summary = "删除评价", description = "根据评价ID删除评价")
    public HttpResult<Boolean> deleteReview(@PathVariable Long reviewId) {
        // 获取当前登录用户
        User currentUser = userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));

        // 调用服务层删除评价
        boolean result = reviewService.deleteReview(reviewId, currentUser);
        
        return HttpResult.success(result);
    }

    @GetMapping("/business/{businessId}/rating")
    @Operation(summary = "获取商家平均评分", description = "根据商家ID获取平均评分")
    public HttpResult<Double> getBusinessRating(@PathVariable Long businessId) {
        // 调用服务层获取商家的平均评分
        Double rating = reviewService.getAverageRatingForBusiness(businessId);
        
        return HttpResult.success(rating);
    }
} 